import os
import zipfile

import wx

if wx.Platform == '__WXMSW__':
    faces = {
        'mono': 'Courier New',
        'size': 8,
    }
else:
    faces = {
        'mono': 'Courier',
        'size': 10,
    }


class PackageDialog(wx.Dialog):

    def _init_sizers(self):
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.usePass = wx.CheckBox(self, label=_("添加密码"))
        self.usePass.Bind(wx.EVT_CHECKBOX, self.onCheck)

        self.editor_sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.lb = wx.StaticText(self, label=_("输入密码"))
        self.iPass = wx.TextCtrl(self, size=wx.Size(100, 20))
        self.iPass.Show(False)
        self.iPass.Bind(wx.EVT_CHAR, self.doCheck)

        self.doPack = wx.Button(self, label=_("执行打包"))
        self.doPack.Bind(wx.EVT_BUTTON, self.Pack)
        self.mainSizer.Add(self.usePass, border=5, flag=wx.ALL)

        self.editor_sizer.Add(self.lb, border=5, flag=wx.ALL)
        self.editor_sizer.Add(self.iPass, border=5, flag=wx.ALL)
        self.mainSizer.Add(self.editor_sizer, border=5, flag=wx.ALL)
        self.mainSizer.Add(self.doPack, border=5, flag=wx.EXPAND | wx.ALL, )
        self.SetSizer(self.mainSizer)
        self.Layout()
        self.Fit()

    def __init__(self, parent, ctr, src):
        self.src = src
        self.ctr = ctr

        wx.Dialog.__init__(self, name='PackageDialog', parent=parent, title=_('Package Project'))
        self._init_sizers()
        self.CenterOnParent()

    def doCheck(self, event):
        if self.usePass.IsChecked() and len(self.iPass.GetValue()) < 4:
            self.doPack.Enable(False)
        else:
            self.doPack.Enable(True)

    def onCheck(self, event):
        if self.usePass.IsChecked():
            self.lb.Show()
            self.iPass.Show()
        else:
            self.lb.Show(False)
            self.iPass.Show(False)
        self.doCheck(event)
        self.Layout()

    def Pack(self, event):
        with zipfile.ZipFile(os.path.join(self.ctr.ProjectPath, 'project.zip'), 'w', zipfile.ZIP_DEFLATED) as fb:
            for i in self.src:
                fb.write(i)
            if self.usePass.IsChecked():
                fb.setpassword(self.iPass.GetValue())
        dialog = wx.MessageDialog(self, _("打包完成!"), _("OK"), wx.OK)
        dialog.ShowModal()
        self.Close()

    def output(self, txt):
        pass
        # self.LogConsole.AppendText(txt)
